$(function () {

    //变量抽取
    var company_dataGrid = $("#company_dataGrid");
    var company_dialog = $("#company_dialog");
    var company_form = $("#company_form");

    company_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/company/query.do",
        fit: true,
        columns: [[
            {field: 'sn', title: '机构代号', width: 100},
            {field: 'name', title: '机构名称', width: 100},
            {field: 'director', title: '法人代表', width: 100},
            {field: 'identity', title: '法人身份证', width: 100},
            {field: 'tel', title: '联系电话', width: 100},
            {field: 'address', title: '联系地址', width: 100},
            {
                field: 'state', title: '合作状态', width: 100, formatter: function (value, row, index) {
                return value ? "<span style='color: blue'>合作</span>" : "<span style='color: orangered'>解除</span>";
            }
            }
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true,//只允许选择一行
        onClickRow: function (index, row) {
            if (row.state) {
                $("#state_btn").linkbutton('enable');

            } else {
                $("#state_btn").linkbutton('disable');
            }
        }
    });

    company_dialog.dialog({
        width: 280,
        height: 340,
        buttons: "#btns",
        closed: true,
        onClose: function () {
            company_form.form("clear");
        },
        resizeable:true
    })

    //方法抽成对象统一管理
    var methodObj = {
        searchForm: function () {
            var keyword = $("#keyword").textbox('getValue');
            var state = $("#state").combobox('getValue');

            company_dataGrid.datagrid('load', {
                keyword: keyword,
                state:state
            });
        },

        add: function () {
            company_dialog.dialog("setTitle", "新增机构");
            company_dialog.dialog("open");

        },
        edit: function () {
            var row = company_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中编辑数据', 'info');
                return;
            }

            //回显数据
            company_form.form("load", row);
            company_dialog.dialog("setTitle", "编辑机构");
            company_dialog.dialog("open");
        },
        changState: function () {
            var row = company_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中一条数据', 'info');
                return;
            }
            $.messager.confirm('提示', '确认更改该数据状态么?', function (r) {
                if (r) {
                    $.get("/company/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('提示', '操作成功', 'info', function () {
                                company_dataGrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('提示', data.msg, 'error')
                        }
                    })
                }
            })

        },
        reload: function () {
            company_dataGrid.datagrid("load");
        },
        save: function () {
            company_form.form('submit', {
                url: "/company/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            methodObj.cancel();
                            company_dataGrid.datagrid("load");
                        })
                    } else {
                        $.messager.alert("提示", "操作失败", "error");
                    }
                }
            })
        },
        cancel: function () {
            company_dialog.dialog("close");
        }
    }

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
})
